Mid-term Exam


CS100 Midterm Exam Cover Sheet

Instructors: Laurent Kneip, Jie Zheng, Fu Song

November 16, 2018

INSTRUCTIONS:

  • You have 90 minutes (~10:25-11:55) to complete the exam.
  • Your exam will not be graded unless you complete the above section and the cover sheet, and turn in both this exam book and the cover sheet.
  • This exam is closed-book and closed-notes, and no electronic devices are permitted.
  • Mark your answers on the exam itself. We will not grade answers written on scratch paper.
  • Your performance is supposed to reflect your own level of understanding of the material. You are not allowed to talk with your neighbor or look at his exam sheet. Failure to obey this rule will result in a point deduction.
  • If you finish early, you can hand in the exam and leave early. However, this is only possible until at latest 15 minutes before the ending time of the exam. If less than 15 minutes are left, please keep sitting and wait until the end.

Problem 1: Multiple-choice questions (4 points)

Please answer the following questions by ticking the choices that apply. Note that multiple choices are possible for each question. Mark all choices that apply as follows: [x].


Question 1 (C)
?/? point (graded)
Consider the following C program:

void f(int, short);
int main()
{
    int i = 100;
    short s = 12;
    short *p = &s;
    ___________; // call to f() 
    return 0;
}
void f(int i, short s)
{
    i++;
}
Which of the following expression(s), when inserted in the blank line above, will result in a compile-time error?


Question 2 (C)
?/? point (graded)
In the following C program:

#include <stdio.h> 
int main(void)
{
    int arr[10][10][10];
    arr[5][5][5] = 2018;
    ___________ // printf statement here 
    return 0;
}        
Which of the following printf statement(s), when inserted in the blank line, would print 2018?


Question 3 (C++)
?/? point (graded)
What procedures of a parent class will be accessible in a child class?


Question 4 (C++)
?/? point (graded)
Choose all of the following declarations that correctly overload a procedure with the declaration float add( float input );


Problem 2: Knowledge questions (4 points)


Question 1 (C)
?/? point (graded)
How many bytes are required to store the string literal “I am good.”? Briefly explain your answer.

(You only need to type a simple number below.)

Answer

11 bytes. The string consists of 10 visible characters (including blank spaces and the period) plus the null character ‘\0’ at the end.


Question 2 (C)
?/? point (graded)
Name the two typical ways for parameter passing between functions in programming.

Please type in ONE word with all lower-case letters in each box.

1. Call-by-

2. Call-by-


Question 3 (C++)
?/? point (graded)
Mention the two types of “Has-a” Relationships viewed in class.

Please type in ONE word with all lower-case letters in each box.

1.

2.


Question 4 (C++)

Put down a one-sentence statement explaining the purpose of include-guards.




Problem 3:


Question 1 (C)
?/? point (graded)
What is the output of the following C code?

#include <stdio.h>
int f(int n);
int main()
{
    printf(“Result = %d\n”, f(5));
    return 0;
}

int f(int n)
{
    int a, b;
    if (n > 2) {
        a = f(n-1);
        b = f(n-2);
        printf(“a = %d, b = %d\n”, a, b);
        return a + b;
    } else {
        return 2;
    }
}                

Warning: Additional space or enter will lead to failure of judgement.


Problem 4:


Question 1 (C)
?/? point (graded)
Circle the errors in the following function written in C, and rewrite the correct function in your answer book. Briefly explain the changes that you made to the original function.

1.  // This function reverses the characters in a string 
2.  void reverse(char *s)
3.  {
4.      int n;
5.      char *tmp, *p, *q; 
6.      n = strlen(s);
7.      q = (n > 0) ? (s+n) : s;
8.      for (p=s; p < q; ++p, --q) {
9.          *tmp = *p;
10.         *p = *q;
11.         *q = *tmp;
12.     }
13. }            
Please type in the line numbers of the potential bugs. Just the number is okay. (Each box one number)
If there is a variable has errors in multiple places, type in the line number the error occurs for the first time.
The line number should NOT contain zero(s) ahead. e.g. You should put "1" instead of "01" if line 1 has a bug.

Answer

Error 1: The pointer variable tmp is used without initialization. It should be changed to a char variable. (Note: Some compilers may produce working code. But it is still an error.)
Error 2: When n is > 0, assigning pointer variable q to (s+n) will make it point to the null character, rather than the last visible character in the string. Thus, (s+n) should be replaced by (s+n-1).
An example of a correct function is:


// This function reverses the characters in a string
void reverse(char *s)
{
    int n;
    char tmp;
    char *p, *q;
    n = strlen(s);
    q = (n > 0) ? (s+n-1) : s;
    for (p=s; p < q; ++p, --q) { 
        tmp = *p;
        *p = *q;
        *q = tmp;
    }
}


Problem 5: Inheritance and polymorphism (C++)

Imagine the following base class:


class Shape {
public:
    Shape();
    virtual ~Shape();
    
    virtual float Area() = 0;
};

Shape::Shape(){}
Shape::~Shape(){}            

a) Write down the declaration of a child class Square that inherits from the above abstract parent class Shape. The child class takes exactly one float parameter in the constructor, which denotes the edge length of the square. You may assume that everything is in one file, so you do not have to worry about header inclusion etc.

b) Write down the implementation of all procedures in the child class (including constructors and destructors).


Additional Resources


The answer of the whole exam can be fond here.

The question sheet can be fond here.